package com.macro.mall.controller;

import com.google.common.collect.Maps;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.CaseDto;
import com.macro.mall.dto.TenantUserDto;
import com.macro.mall.model.CaseEntity;
import com.macro.mall.model.CasePicEntity;
import com.macro.mall.model.TenantEntity;
import com.macro.mall.model.caseStyleEntity;
import com.macro.mall.service.CaseService;
import com.macro.mall.service.TenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *  案例管理controller
 */
@RestController
@Api(tags = "CaseController", description = "案例管理后台")
@RequestMapping("/case")
public class CaseController {
    @Autowired
    private CaseService caseService;

    @Autowired
    private TenantService tenantService;

    @ApiOperation(value = "查询案例列表")
    @RequestMapping(value = "/findCaseList", method = RequestMethod.POST)
    public CommonResult findCaseList(HttpServletRequest request, @RequestBody TenantUserDto tenantUserDto){
        Long tenantId = tenantUserDto.getTenantId();
        if(tenantId == null){
            String userName = String.valueOf(request.getAttribute("useName"));
            TenantEntity tenantInfo = tenantService.findTenantInfo(userName);
            tenantId = tenantInfo.getId();
        }
        int caseCount = caseService.findCaseCount(tenantId);
        Integer pageSize = tenantUserDto.getPageSize();
        Integer curPage = tenantUserDto.getPageNo();
        List<CaseEntity> caseList = null;
        if(caseCount > 0){
             caseList = caseService.findCaseList(tenantId, pageSize, curPage);
        }
        Map<Object, Object> map = Maps.newHashMap();
        map.put("list", caseList);
        map.put("pageNum", curPage);
        map.put("pageSize", pageSize);
        map.put("total", caseCount);
        Integer totalPage = (caseCount+1)/pageSize + 1;
        map.put("totalPage", totalPage);
        return CommonResult.success(map);
    }

    @ApiOperation(value = "查询案例列表详情")
    @RequestMapping(value = "/findCaseInfo/{caseId}", method = RequestMethod.GET)
    public CommonResult findCaseInfo(@PathVariable(value = "caseId") Long caseId){
        CasePicEntity caseInfo = caseService.findCaseInfo(caseId);
        return CommonResult.success(caseInfo);
    }

    @ApiOperation(value = "删除案例")
    @RequestMapping(value = "/deleteCase/{caseId}", method = RequestMethod.GET)
    public CommonResult deleteCase(@PathVariable("caseId") Long caseId){
        caseService.deleteCase(caseId);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "新增案例")
    @RequestMapping(value = "/addCase", method = RequestMethod.POST)
    public CommonResult addCase(HttpServletRequest request,@RequestBody CaseDto caseDto){
        String userName = String.valueOf(request.getAttribute("useName"));
        TenantEntity tenantInfo = tenantService.findTenantInfo(userName);
        Long tenantId = tenantInfo.getId();
        caseDto.setTenantId(tenantId);
        caseService.addCase(caseDto);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "修改案例")
    @RequestMapping(value = "/updateCase", method = RequestMethod.POST)
    public CommonResult updateCase(@RequestBody CaseDto caseDto){
        caseService.updateCase(caseDto);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "查询风格")
    @RequestMapping(value = "/findCaseStyleEntity", method = RequestMethod.GET)
    public CommonResult findCaseStyleEntity(){
        List<caseStyleEntity> caseStyleEntity = caseService.findCaseStyleEntity();
        return CommonResult.success(caseStyleEntity);
    }

    @ApiOperation(value = "查询类别")
    @RequestMapping(value = "/findCaseTypeList", method = RequestMethod.GET)
    public CommonResult findCaseTypeList(){
        List<caseStyleEntity> caseTypeList = caseService.findCaseTypeList();
        return CommonResult.success(caseTypeList);
    }
}
